import {
  getforsendInfo,
  delivery,
  downOrder,
  printerSend,
  JuJueJieDan,
  JieDan
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openType: "zijifahuo",
    dataInfo: null,
    expressNo: "",
    GMoney: "",
    oid: "",
    totalRemark: "",
    printering: false,
    timeIndex: "",
    showFaHuoTimePop: false,
    checkTimeId: "",
    memo:""
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    if (options.openType) {
      this.setData({
        openType: options.openType
      })
    }

    getforsendInfo(options.oid, (r) => {
      if (r.code == 0) {
        let item = options.item ? JSON.parse(options.item) : null;
        if (item) {
          r.data.supplier = item
        }
        r.data.deliver_option[0].isSelect = true;
        debugger;
        let title=""
        switch (r.data.tab_state){
          case 1: title = "接单"; break;
          case 2: title = "等待供应商接单"; break;
          case 3: title = "等待发货"; break;
          default: title = "去发货"; break;
        } 
        wx.setNavigationBarTitle({
          title: title,
        })
        if (!r.data.mine) {
          wx.hideShareMenu();
        }
        self.setData({
          dataInfo: r.data,
          oid: options.oid,
          expressNo: r.data.order.express_no,
          checkTimeId: r.data.deliver_option[0].id
        })

      } else {
        errorMsg(r.message)
      }
    })

  },

  //选择预计发货时间
  bindPickerChange2: function(e) {
    this.setData({
      timeIndex: e.detail.value,

    })
  },
  goSelectSeller: function() {
    wx.navigateTo({
      url: '../../openbillbox/selectseller/selectseller?openType=xiadan&oid=' + this.data.oid,
    })
  },
  onBack: function() {
    wx.navigateBack();
  },
  //发货
  // onDelivery: function() {
  //   let {
  //     dataInfo,
  //     GMoney,
  //     expressNo
  //   } = this.data
  //   //my_order_id, money, express_no
  //   if (expressNo == "") {
  //     errorMsg("请输入快递单号");
  //   }   else {
  //     delivery(dataInfo.my_order_id, GMoney, expressNo, (r) => {
  //       if (r.code == 0) {
  //         wx.redirectTo({
  //           url: '../deliverysuccess/deliverysuccess?dataInfo=' + JSON.stringify(r.data),
  //         })
  //       } else {
  //         errorMsg(r.message);
  //       }
  //     })
  //   } 
  // },
  //form发货
  formSubmit: function(e) {
    let {
      openType
    } = this.data;

    let {
      dataInfo,
      GMoney,
      totalRemark,
      expressNo,
      timeIndex
    } = this.data;
    if (openType == 'zijifahuo') {
      if (expressNo == "") {
        errorMsg("请输入快递单号");
      } else {
        delivery(dataInfo.my_order_id, GMoney, expressNo, e.detail.formId, timeIndex ? dataInfo.deliver_option[timeIndex].id : "", (r) => {
          if (r.code == 0) {
            wx.redirectTo({
              url: '../deliverysuccess/deliverysuccess?dataInfo=' + escape(JSON.stringify(r.data)),
            })
          } else {
            errorMsg(r.message);
          }
        })
      }
    } else if (openType == 'tarenfahuo') {


      downOrder(dataInfo.my_order_id, dataInfo.supplier.id, GMoney, totalRemark, e.detail.formId, (r) => {
        if (r.code == 0) {
          wx.navigateBack({
            delta: 3
          })
          errorMsg("下单成功");
        } else {
          errorMsg(r.message)
        }
      })

    }



  },




  //将订单发送给上级
  onSendOrder: function() {
    let {
      dataInfo,
      GMoney,
      totalRemark
    } = this.data;

    downOrder(dataInfo.my_order_id, dataInfo.supplier.id, GMoney, totalRemark, (r) => {
      if (r.code == 0) {
        wx.navigateBack({
          delta: 3
        })
        setTimeout(() => {
          errorMsg("下单成功");
        }, 500)

      } else {
        errorMsg(r.message)
      }
    })

  },
  //获取快递单号
  getExpressNo: function(e) {
    this.setData({
      expressNo: e.detail.value
    })
  },
  //获取供货价格
  getGMoney: function(e) {
    this.setData({
      GMoney: e.detail.value
    })
  },
  //扫一扫获取二维码
  onScanCode: function() {
    wx.scanCode({
      success: (res) => {
        this.setData({
          expressNo: res.result
        })
      },
      fail: (res) => {
        errorMsg("识别失败")
      }
    }) 
  },
  //图片预览
  previewImage: function(e) {
    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current[0], // 当前显示图片的http链接		  	
      urls: current // 需要预览的图片http链接列表	
    })
  },
  //修改备注
  editTotalRemark: function() {
    let {
      dataInfo,
      totalRemark
    } = this.data
    wx.navigateTo({
      url: '../../openbillbox/totalremark/totalremark?totalRemarkPla=' + dataInfo.memo + "&totalRemark=" + totalRemark,
    })
  },
  getMemo:function(e){
   this.setData({
     memo:e.detail.value
   })
  },
  //复制
  onCopy: function(e) {
    let {
      name,
      mobile,
      address
    } = e.currentTarget.dataset;
    wx.setClipboardData({
      data: "收件人:" + name + "\r\n" + "电话:" + mobile + "\r\n" + "地址:" + address + "\r\n",
      success() {
        errorMsg("复制成功")
      }
    })
  },
  //打印订单
  onPrinter: function() {

    let {
      printering
    } = this.data;
    let self = this;
    if (!printering) {
      this.setData({
        printering: true,
      })
      if (this.data.dataInfo) {
        printerSend(this.data.dataInfo.my_order_id, (r) => {
          if (r.code == 0) {
            self.setData({
              expressNo: r.data,
              printering: false,
            })
            errorMsg(r.message);
          } else {
            self.setData({
              printering: false,
            })
            errorMsg(r.message);
          }
        })
      }

    }

  },
  //点击接单
  onJieDan: function() {
    this.setData({
      showFaHuoTimePop: true
    })
  },

  //关闭弹窗
  popClose: function() {
    this.setData({
      showFaHuoTimePop: false
    })
  },
  //选择预计发货时间
  checkTime: function(e) {
    let id = e.currentTarget.dataset.id;

    let {
      dataInfo,
      checkTimeId
    } = this.data;
    dataInfo.deliver_option.map((o, i) => {
      if (o.id == id) {
        o.isSelect = true;
        checkTimeId = o.id;
      } else {
        o.isSelect = false;
      }
    })

    this.setData({
      ['dataInfo.deliver_option']: dataInfo.deliver_option,
      checkTimeId: checkTimeId
    })

  },
  //点击确认接单
  onJieDanSubmit: function() {
    let {
      checkTimeId,
      dataInfo,
      memo
    } = this.data;
    JieDan(dataInfo.my_order_id, checkTimeId,memo, (r) => {
      if (r.code == 0) {
        wx.navigateBack();
        setTimeout(() => {
          errorMsg(r.message);
        }, 500)
      } else {
        errorMsg(r.message);
      }
    })

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '请核对您的订单，点击实时查询物流信息',
      imageUrl: this.data.dataInfo.order.items[0].imgSrc,
      path: "pages/myorderbox/vieworderdetail/vieworderdetail?orderId=" + this.data.dataInfo.order_id
    }


  },
  onJuJue: function() {
    let {
      dataInfo
    } = this.data;


    wx.showModal({
      title: '拒绝接单',
      content: '确定要拒绝该订单吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          JuJueJieDan(dataInfo.my_order_id, "", (r) => {
            if (r.code == 0) {
              wx.navigateBack()
              setTimeout(() => {
                errorMsg(r.message);
              }, 500)
            } else {
              errorMsg(r.message);
            }
          })
        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  //修改备注
  editRemark: function(e) {
    let { dataInfo}  = this.data;
    if (dataInfo.mine && (dataInfo.tab_state == 2 || dataInfo.tab_state == 3)){
    let remark = e.currentTarget.dataset.remark
    let id = e.currentTarget.dataset.id
    let orderid = e.currentTarget.dataset.orderid
    debugger;
    wx.navigateTo({
      url: '../../openbillbox/totalremark/totalremark?totalRemark=' + remark + "&id=" + id + "&orderid=" + orderid,
    })
  }
    
  }
})